<template>
  <div class="container">
    <div class="btnList">
      <el-button type="info" size="small" icon="el-icon-edit" @click="amendType"
        >修改供应商状态</el-button
      >
      <el-button
        type="success"
        size="small"
        icon="el-icon-check"
        @click="synErp"
        :disabled="!bool"
        >同步ERP</el-button
      >
      <el-button
        type="danger"
        size="small"
        icon="el-icon-delete"
        @click="expurgate"
        >删除</el-button
      >
      <el-select
        class="select"
        v-model="selectVal"
        size="medium"
        @change="tabMode()"
        placeholder="请选择"
      >
        <el-option
          v-for="item in selectMain"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <!-- <el-button
        class="add"
        size="small"
        type="primary"
        icon="el-icon-circle-plus"
        @click="addSupplier"
      >添加供应商</el-button>-->
    </div>
    <div class="top">
      <el-select
        v-model="search.area"
        filterable
        remote
        center
        reserve-keyword
        placeholder="请输入区域"
        :remote-method="insideRemoteMethods"
        :loading="insideArea.loading"
      >
        <el-option
          v-for="item in insideArea.options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索供应商信息"
        v-model="supplierNameList.name"
      >
        <template slot="prepend">供应商</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="supplierBtn"
        ></el-button>
      </el-input>
      <!--<el-input-->
      <!--style="width:200px;margin-bottom:5px"-->
      <!--v-model="search.searchName"-->
      <!--placeholder="请输入供应商名称"-->
      <!--clearable-->
      <!--&gt;</el-input>-->
      <!--<el-input-->
      <!--style="width:200px;margin-bottom:5px"-->
      <!--v-model="search.searchCode"-->
      <!--placeholder="请输入供应商编码"-->
      <!--clearable-->
      <!--&gt;</el-input>-->
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        type="danger"
        size="small"
        icon="el-icon-delete"
        class="handleSearch"
        @click="empty()"
        >清空</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="code"
        label="供应商编号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="tax_amount"
        label="税票金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="create_time"
        label="创建日期"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="is_agricultureMsg"
        label="是否为现代农业"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="states"
        label="有效状态"
        align="center"
      ></el-table-column>
      <el-table-column label="返利返点比" align="center" width="200">
        <template slot-scope="scope">
          <el-input
            style="width: 100px"
            v-model="scope.row.reb"
            @change="rebNum(scope)"
            placeholder="请输入返点比"
          ></el-input>
          %
        </template>
      </el-table-column>
      <el-table-column label="物流返点比" align="center" width="200">
        <template slot-scope="scope">
          <el-input
            style="width: 100px"
            v-model="scope.row.wl_reb"
            @change="rebNum1(scope)"
            placeholder="请输入物流返点比"
          ></el-input>
          %
        </template>
      </el-table-column>
      <el-table-column label="供应商用友编码" align="center" width="200">
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.yy_code"
            @change="yyCode(scope)"
            placeholder="请输入供应商用友编码"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column label="结算单初始是否冻结" align="center" width="180">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.statment_freezeMsg"
            active-text="冻结"
            inactive-text="不冻结"
            @change="isStatment_freeze(scope)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="420" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="modification(scope)"
            >修改银行卡信息</el-button
          >
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-circle-plus"
            v-if="scope.row.is_agriculture"
            @click="addTax(scope)"
            >添加税票</el-button
          >
          <el-button
            size="mini"
            v-if="scope.row.is_agriculture"
            type="info"
            icon="el-icon-info"
            @click="taxParticulars(scope)"
            >税票详情</el-button
          >
          <!-- <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleEdit(scope)">编辑</el-button> -->
          <el-button
            type="warning"
            size="mini"
            icon="el-icon-edit-outline"
            @click="binding(scope)"
            >绑定商品</el-button
          >
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            v-if="scope.row.must_pay == 0"
            @click="payment(scope)"
            >设置必付</el-button
          >
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-edit"
            v-if="scope.row.must_pay == 1"
            @click="cancelPayment(scope)"
            >取消必付</el-button
          >
          <el-button
            size="mini"
            type="success"
            icon="el-icon-menu"
            @click="batch(scope)"
            >商品批次</el-button
          >
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="particulars(scope)"
            >详情</el-button
          >
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="expurgate(scope)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="height: 20px">
      <!-- dialog -->
      <supplierDialog @upData="getData" :dialog="dialog"></supplierDialog>
      <!-- 修改供应商状态dialog -->
      <supplierTypeDialog
        @upData="getData"
        :multipleSelection="multipleSelection"
        :typeDialog="typeDialog"
      ></supplierTypeDialog>
      <!-- 绑定商品 -->
      <bindingDialog
        @upData="getData"
        :bindingDialog="bindingDialog"
      ></bindingDialog>
      <!-- 详情 -->
      <supplierDetailsDialog
        :detailsDialog="detailsDialog"
      ></supplierDetailsDialog>
      <!-- 添加税票 -->
      <taxDialog :taxDialogs="taxDialogs" @upData="getData"></taxDialog>
      <!-- 税票详情 -->
      <taxParincularDialog
        :taxParticularsDialogs="taxParticularsDialogs"
      ></taxParincularDialog>
      <!-- 批次 -->
      <bactchDialog :bactchDialogs="bactchDialogs"></bactchDialog>
    </div>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <supplierSearch
      :supplierdialog="supplierdialog"
      @tag="supplierTag"
    ></supplierSearch>
    <supplierModificationDialog
      :dialog="modificationDialog"
      @upData="getData"
    ></supplierModificationDialog>
  </div>
</template>
<script>
import { getAreaList } from "@/api/permission/adminlist";
import SupplierDialog from "@/components/supplier/supplierDialog";
import TaxDialog from "@/components/supplier/taxDialog";
import TaxParincularDialog from "@/components/supplier/taxParincularDialog";
import BactchDialog from "@/components/supplier/bactchDialog";
import SupplierTypeDialog from "@/components/supplier/supplierTypeDialog";
import SupplierSearch from "@/components/grossMargin/supplierSearch";
import BindingDialog from "@/components/supplier/bindingDialog";
import SupplierDetailsDialog from "@/components/supplier/supplierDetailsDialog";
import SupplierModificationDialog from "@/components/supplier/supplierModificationDialog";
import axios from "axios";
import { parseTime } from "@/utils/index";
import Pagination from "@/components/pagination";
import { getToken } from "@/utils/auth";
import {
  supplierList,
  delSupplier,
  synSupplier,
  setyyCode,
  setReb,
  setMustPay,
  changeStatmentInitFreezeStatus,
} from "@/api/supplier/supplierList";
export default {
  components: {
    pagination: Pagination,
    taxDialog: TaxDialog,
    taxParincularDialog: TaxParincularDialog,
    supplierDialog: SupplierDialog,
    supplierTypeDialog: SupplierTypeDialog,
    bindingDialog: BindingDialog,
    supplierDetailsDialog: SupplierDetailsDialog,
    bactchDialog: BactchDialog,
    supplierSearch: SupplierSearch,
    supplierModificationDialog: SupplierModificationDialog,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //切换本地或者ERP
      selectMain: [
        {
          value: 0,
          label: "本地",
        },
        {
          value: 1,
          label: "ERP",
        },
      ],
      selectVal: "本地",
      dialog: {
        centerDialogVisible: false,
        dialogType: "",
        id: "",
      },
      insideArea: {
        innerVisible: false,
        options: [],
        value: "",
        list: [],
        loading: false,
      },
      taxDialogs: {
        centerDialogVisible: false,
        id: "",
      },
      taxParticularsDialogs: {
        centerDialogVisible: false,
        id: "",
      },
      //修改供应商状态
      typeDialog: {
        dialogType: "",
        centerDialogVisible: false,
      },
      supplierdialog: {
        centerDialogVisible: false,
      },

      // 详情
      detailsDialog: {
        id: "",
        centerDialogVisible: false,
      },
      //修改银行卡信息
      modificationDialog: {
        centerDialogVisible: false,
        formData: {},
      },
      // 批次
      bactchDialogs: {
        id: "",
        centerDialogVisible: false,
      },
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      formData: {},
      search: {
        // searchCode: "",
        // searchName: ""
        supplier_id: "",
        area: "",
      },
      multipleSelection: [],
      bindingDialog: {
        centerDialogVisible: false,
        id: "",
      },
      supplierNameList: {
        name: "",
        code: "",
      },
      bool: true,
    };
  },

  created() {
    //页面数据
    this.getData();
    this.getDatas();
  },
  methods: {
    //页面数据
    getData() {
      this.multipleSelection = [];
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        type: this.selectVal == 1 ? 1 : 0,
        // code: this.search.searchCode,
        // name: this.search.searchName,
        supplier_id: this.supplierNameList.code,
        orgarea_code_str: this.search.area,
      };
      supplierList(data)
        .then((res) => {
          if (res.code == 0) {
            res.data.data.forEach((res) => {
              res.create_at = parseTime(res.add_time);
              res.update_at = parseTime(res.update_time);
              res.is_agricultureMsg = res.is_agriculture == 1 ? "是" : "不是";
              res.statment_freezeMsg = res.statment_freeze == 1 ? true : false;
            });
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getDatas() {
      let data = {
        page: 1,
        limit: 50,
      };

      //获取初始化区域列表
      getAreaList(data).then((res) => {
        this.insideArea.options = res.data.listdata.map((item) => {
          return { value: `${item.code}`, label: `${item.name}` };
        });
      });
    },
    // 搜索区域
    insideRemoteMethods(query) {
      if (query !== "") {
        this.getSupplierLists(query);
      } else {
        this.insideArea.options = [];
      }
    },
    getSupplierLists(query) {
      let data = {
        name: query,
        page: 1,
        limit: 100,
      };
      getAreaList(data).then((res) => {
        this.insideArea.list = res.data.listdata.map((item) => {
          return { value: `${item.code}`, label: `${item.name}` };
        });
        this.insideArea.options = this.insideArea.list.filter((item) => {
          this.insideArea.loading = false;
          return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
        });
      });
    },
    supplierTag(data) {
      this.supplierNameList = {
        name: "",
        code: "",
      };
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.id);
      });
      this.supplierNameList.name = arrName.join(",");
      this.supplierNameList.code = arrCode.join(",");
    },
    //用友编码
    yyCode(scope) {
      let data = {
        id: scope.row.id,
        yy_code: scope.row.yy_code,
      };
      setyyCode(data).then((res) => {
        if (res.code == 0) {
          this.$message({
            type: "success",
            message: res.msg || res.message,
          });
        } else {
          this.$message({
            type: "error",
            message: res.msg || res.message,
          });
          this.getData();
        }
      });
    },
    // 返利返点比
    rebNum(scope) {
      let data = {
        id: scope.row.id,
        reb: scope.row.reb,
        type: 1,
      };
      setReb(data).then((res) => {
        if (res.code == 0) {
          this.$message({
            type: "success",
            message: res.msg || res.message,
          });
        } else {
          this.$message({
            type: "error",
            message: res.msg || res.message,
          });
          this.getData();
        }
      });
    },
    // 物流返点比
    rebNum1(scope) {
      let data = {
        id: scope.row.id,
        reb: scope.row.wl_reb,
        type: 2,
      };
      setReb(data).then((res) => {
        if (res.code == 0) {
          this.$message({
            type: "success",
            message: res.msg || res.message,
          });
        } else {
          this.$message({
            type: "error",
            message: res.msg || res.message,
          });
          this.getData();
        }
      });
    },
    //超级供应商搜索
    supplierBtn() {
      this.supplierdialog = {
        centerDialogVisible: true,
      };
    },
    modification(scope) {
      this.modificationDialog = {
        centerDialogVisible: true,
        formData: scope.row,
        id: scope.row.id,
      };
    },
    // 修改供应商状态
    amendType() {
      if (this.multipleSelection.length == 0) {
        this.$message({
          type: "info",
          message: "请选择供应商",
        });
      } else {
        this.typeDialog.centerDialogVisible = true;
      }
    },
    //添加税票
    addTax(scope) {
      this.taxDialogs.centerDialogVisible = true;
      this.taxDialogs.id = scope.row.id;
    },
    //税票详情
    taxParticulars(scope) {
      console.log(23232);
      this.taxParticularsDialogs.centerDialogVisible = true;
      this.taxParticularsDialogs.id = scope.row.id;
    },
    // 编辑
    handleEdit(scope) {
      this.dialog.centerDialogVisible = true;
      this.dialog.dialogType = "edit";
      this.dialog.id = scope.row.id;
    },
    //详情
    particulars(scope) {
      this.detailsDialog.id = scope.row.id;
      this.detailsDialog.centerDialogVisible = true;
    },
    // 删除
    expurgate(scope) {
      let data = null;
      // 判断是多选还是单选
      if (scope.row) {
        data = {
          id: scope.row.id,
        };
      } else {
        data = {
          id: this.multipleSelection,
        };
      }
      this.$confirm("是否删除该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delSupplier(data).then((res) => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 同步ERP
    synErp() {
      this.$confirm("是否同步ERP信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.bool = false;
          let storage = window.localStorage;
          storage.setItem("date", 36000000);
          let time = storage.getItem("date");

          let clearTime = setInterval(() => {
            if (time == 0) {
              this.bool = true;
              clearInterval(clearTime);
            } else {
              time -= 1000;
              storage.setItem("date", time);
              console.log(time);
            }
          }, 1000);
          axios.defaults.headers.common["X-Token"] = getToken();
          axios.post(`/api/Supplier/synSupplier`).then((res) => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.data.msg,
              });
            } else {
              this.$message({
                type: "error",
                message: res.data.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //结算单初始是否冻结
    isStatment_freeze(scope) {
      // 判断是多选还是单选
      let data = {
        id: scope.row.id,
        statment_freeze: scope.row.statment_freezeMsg == true ? 1 : 0,
      };
      changeStatmentInitFreezeStatus(data).then((res) => {
        if (res.code == 0) {
          this.getData();
          this.$message({
            type: "success",
            message: res.msg,
          });
        }
      });
    },
    //绑定项目
    binding(scope) {
      this.bindingDialog.centerDialogVisible = true;
      this.bindingDialog.id = scope.row.id;
    },
    //添加供应商
    addSupplier() {
      this.dialog.centerDialogVisible = true;
      this.dialog.dialogType = "";
      this.dialog.id = "";
    },
    //取消必付
    cancelPayment(scope) {
      let data = {
        id: scope.row.id,
        must_pay: 0,
      };
      setMustPay(data).then((res) => {
        if (res.code == 0) {
          this.$message({
            type: "success",
            message: res.msg || res.message,
          });
          this.getData();
        } else {
          this.$message({
            type: "error",
            message: res.msg || res.message,
          });
        }
      });
    },
    //设置必付
    payment(scope) {
      let data = {
        id: scope.row.id,
        must_pay: 1,
      };
      setMustPay(data).then((res) => {
        if (res.code == 0) {
          this.$message({
            type: "success",
            message: res.msg || res.message,
          });
          this.getData();
        } else {
          this.$message({
            type: "error",
            message: res.msg || res.message,
          });
        }
      });
    },
    //批次
    batch(scope) {
      this.bactchDialogs.centerDialogVisible = true;
      this.bactchDialogs.id = scope.row.id;
    },
    // 切换本地或者ERP
    tabMode() {
      this.getData();
    },
    // 本地搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //清空
    empty() {
      this.search = {
        // searchCode: "",
        // searchName: ""
        supplier_id: "",
        area: "",
      };
      this.insideArea = {
        innerVisible: false,
        options: [],
        value: "",
        list: [],
        loading: false,
      };
      this.getDatas();
      this.supplierNameList.name = "";
      this.supplierNameList.code = "";
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;

      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = [];
      let valMain = val;
      valMain.forEach((res) => {
        this.multipleSelection.push(res.id);
      });
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
}
.btnList {
  position: relative;
  margin-bottom: 5px;
}
.add {
  position: absolute;
  right: 0;
  top: 0;
}
.select {
  width: 80px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
/*.el-button {*/
/*margin-bottom: 5px;*/
/*}*/
</style>
